﻿#include "axisworkmodewidget.h"

QAxisWorkModeWidget::QAxisWorkModeWidget(int axisNo,QWidget *parent) : QWidget(parent)
{
    _i32FrameCnt = 0;

    this->setMinimumHeight(400);
    _i32AixsNo = axisNo;

    InitParamConfig();

    this->setStyleSheet("background-color:transparent;");

    InitUI();


    void (QButtonGroup::*fn1)(int) = &QButtonGroup::buttonClicked;
    connect(_pMotorEnableBtnGroup,fn1,this,[=](int nindex){
        if(nindex == 0x01)//开使能
        {
            _pProcessWidget->setEnabled(true);
        }
        if(nindex == 0x02)//关使能
        {
            _pProcessWidget->setEnabled(false);
        }

        int l_i32cmdid = nindex==0x01?0x1A:0x1B;
        emit signal_setparam(_i32AixsNo,l_i32cmdid,0x0,1);
    });


    _pProcessWidget->setEnabled(false);

}

/**
* @brief 初始化设置的参数
* @note
*
*/
void QAxisWorkModeWidget::InitParamConfig()
{
    //操作命令
    st_cmd_attr l_cmd_attr;
    l_cmd_attr.sName = "位置给定";
    l_cmd_attr.i32Cmdid = 0x2A;
    l_cmd_attr.lfCoeff = 16777216/360.0;
    l_cmd_attr.sUnit = "°";
    l_cmd_attr.i32Valuelen = 3;
    l_cmd_attr.lfMaxValue = 360.0;
    l_cmd_attr.lfMinValue = 0.0;
    l_cmd_attr.ptrValue = nullptr;
    _processcmdMap.insert(0,l_cmd_attr);

    l_cmd_attr.sName = "速度给定";
    l_cmd_attr.i32Cmdid = 0x25;
    l_cmd_attr.lfCoeff = 100.0;
    l_cmd_attr.sUnit = "°/s";
    l_cmd_attr.i32Valuelen = 2;
    l_cmd_attr.lfMaxValue = 10.0;
    l_cmd_attr.lfMinValue = -10.0;
    l_cmd_attr.bsigned = true;
    if(_i32AixsNo == M_AXIS_AZI)
    {
        l_cmd_attr.ptrValue = &_rcvdata_11.azimuth_speed_response_value.data[0];
    }
    if(_i32AixsNo == M_AXIS_PICTH)
    {
        l_cmd_attr.ptrValue = &_rcvdata_11.pitch_speed_response_value.data[0];
    }
    _processcmdMap.insert(1,l_cmd_attr);

    l_cmd_attr.sName = "电流给定";
    l_cmd_attr.i32Cmdid = 0x2F;
    l_cmd_attr.lfCoeff = 10000.0;
    l_cmd_attr.sUnit = "°/s";
    l_cmd_attr.i32Valuelen = 2;
    l_cmd_attr.lfMaxValue = 3.0;
    l_cmd_attr.lfMinValue = -3.0;
    l_cmd_attr.bsigned = true;
    if(_i32AixsNo == M_AXIS_AZI)
    {
        l_cmd_attr.ptrValue = &_rcvdata_11.azimuth_currentQ_response_value.data[0];
    }
    if(_i32AixsNo == M_AXIS_PICTH)
    {
        l_cmd_attr.ptrValue = &_rcvdata_11.pitch_currentQ_response_value.data[0];
    }
    _processcmdMap.insert(2,l_cmd_attr);


}
/**
* @brief
* @note
*
*/
 void QAxisWorkModeWidget::InitUI()
 {
     QGridLayout* playout = new QGridLayout(this);
     playout->addWidget(createProcessWidget(),0,0,1,1);
 }

/**
* @brief
* @note
*
*/
QWidget* QAxisWorkModeWidget::createProcessWidget()
{
    _pMotorEnableButton = new QPushButton("开使能");
    _pMotorEnableButton->setCheckable(true);
    _pMotorDisEnableButton = new QPushButton("关使能");
    _pMotorDisEnableButton->setCheckable(true);

    _pMotorEnableBtnGroup = new QButtonGroup(this);
    _pMotorEnableBtnGroup->addButton(_pMotorEnableButton,0x01);
    _pMotorEnableBtnGroup->addButton(_pMotorDisEnableButton,0x02);

    setButtonStyleSheet(_pMotorEnableButton);
    setButtonStyleSheet(_pMotorDisEnableButton);

    //参数设置
    _pProcessWidget = new QTableWidget(this);
    QString sTableStyle = "QTableWidget{border:0px solid;border-top:1px solid rgb(150, 150, 150);";
    sTableStyle = sTableStyle + "font-size:16px;font-family: 'Microsoft YaHei';color:rgb(255,255,255);}";
    sTableStyle = sTableStyle + "QTableWidget::Item{border:0px solid rgb(150, 150, 150);border-bottom:1px solid rgb(150, 150, 150);}";
    _pProcessWidget->setStyleSheet(sTableStyle);
    _pProcessWidget->setShowGrid(false);
//    _pProcessWidget->setStyleSheet("border:0px;font-size:16px;font-family: 'Microsoft YaHei';");
    _pProcessWidget->horizontalHeader()->setStretchLastSection(true);//最后一列充满
    _pProcessWidget->verticalHeader()->hide();
    _pProcessWidget->setColumnCount(4);
    _pProcessWidget->setRowCount(_processcmdMap.size());


    QStringList header;
    header<<"给定模式"<<"设置值"<<"反馈值"<<"操作";
    _pProcessWidget->setHorizontalHeaderLabels(header);
    _pProcessWidget->setColumnWidth(1,100);
    _pProcessWidget->setColumnWidth(2,100);

    _pProcessWidget->setColumnHidden(2,true);//反馈值不需要在此处显示

    for(int i = 0; i < _processcmdMap.keys().size();i++)
    {
        _pProcessWidget->setRowHeight(i,40);

        int  l_key = _processcmdMap.keys()[i];
        QString sName = _processcmdMap[l_key].sName;

        _pProcessWidget->setItem(i,0,new QTableWidgetItem(sName));
        _pProcessWidget->item(i,0)->setFlags(Qt::ItemIsEnabled);
        _pProcessWidget->setItem(i,1,new QTableWidgetItem("0.0"));
        _pProcessWidget->item(i,1)->setToolTip(QString("数据范围:[%1,%2]").arg(_processcmdMap[l_key].lfMinValue).arg(_processcmdMap[l_key].lfMaxValue));
        _pProcessWidget->setItem(i,2,new QTableWidgetItem("0.0"));
        _pProcessWidget->item(i,2)->setFlags(Qt::ItemIsEnabled);

        _pProcessWidget->item(i,1)->setTextAlignment(Qt::AlignCenter);
        _pProcessWidget->item(i,2)->setTextAlignment(Qt::AlignCenter);

        QPushButton* pbutton = new QPushButton("设置",this);
        pbutton->setStyleSheet(
                    "QPushButton{padding:5px;border-radius:5px;color:rgb(255,255,255);font-size:18px;font-family: 'Microsoft YaHei';background-color: rgb(46,97,186);}"
                    "QPushButton:hover{padding:5px;border-radius:5px;color:white;font-size:18px;font-family: 'Microsoft YaHei';background-color: rgb(96,131,203);}"
                    "QPushButton:pressed{padding:5px;border-radius:5px;color:white;font-size:18px;font-family: 'Microsoft YaHei';background-color: rgb(139,165,217);}"
                    "QPushButton:disabled { background-color: rgb(200,200,200);color:black;}"
                                    );
        connect(pbutton,&QPushButton::clicked,this,&QAxisWorkModeWidget::onProcessParamButtonClick);
        _pProcessWidget->setCellWidget(i,3,pbutton);
    }


//    _pParamSetWidget->setSpan(_cmdMap.size()-4,3,3,1);


    QLabel* plabel = new QLabel("电机使能:");
    plabel->setStyleSheet("QLabel{color:rgb(255,255,255);font-size:18px;font-family: 'Microsoft YaHei';}");


    int nRow = 0;

    QWidget* pwidget = new QWidget(this);
    QGridLayout* playout = new QGridLayout(pwidget);

    playout->addWidget(plabel,nRow,0,1,1);
    playout->addWidget(_pMotorEnableButton,nRow,1,1,1);
    playout->addWidget(_pMotorDisEnableButton,nRow,2,1,1);
    nRow++;
    playout->addWidget(_pProcessWidget,nRow,0,1,4);

    return pwidget;
}

/**
* @brief
* @note
*
*/
void QAxisWorkModeWidget::onProcessParamButtonClick()
{
    QPushButton *pushButton = dynamic_cast<QPushButton*>(this->sender());
    if(pushButton == nullptr)
    {
        return;
    }
    int x = pushButton->frameGeometry().x();
    int y = pushButton->frameGeometry().y();
    QModelIndex index = _pProcessWidget->indexAt(QPoint(x, y));
    int row = index.row();

    if(row >=  _processcmdMap.keys().size())
    {
        return;
    }

    //获取当前命令的属性值
    int l_key =  _processcmdMap.keys()[row];
    st_cmd_attr l_cmd_attr = _processcmdMap[l_key];
    int l_cmd_id = l_cmd_attr.i32Cmdid;
    float lfValue = _pProcessWidget->item(row,1)->text().toFloat();
    if(lfValue > l_cmd_attr.lfMaxValue || lfValue < l_cmd_attr.lfMinValue)
    {
        QMessageBox *msgBox = new QMessageBox();
        msgBox->setWindowTitle("提示");
        msgBox->setText(QString("输入数据有误，有效范围为[%1,%2]").arg(l_cmd_attr.lfMinValue).arg(l_cmd_attr.lfMaxValue));
        msgBox->addButton("确定", QMessageBox::AcceptRole);
        msgBox->setStyleSheet("font-size:18px;font-family: 'Microsoft YaHei';background-color:white");
        msgBox->exec();
        delete msgBox;
        return;
    }

    fourchar l_currentvalue;
    l_currentvalue.i32data = lfValue*l_cmd_attr.lfCoeff;

    emit signal_setparam(_i32AixsNo,l_cmd_id,l_currentvalue.i32data,l_cmd_attr.i32Valuelen);

}
/**
* @brief
* @note
*
*/
void QAxisWorkModeWidget::showRcvdata11(_loopdata_11 rcvdata11)
{
    _i32FrameCnt++;
    if(_i32FrameCnt% 50 != 0)
    {
        return;
    }
    _rcvdata_11 = rcvdata11;

    for(int i = 0;i < _processcmdMap.keys().size();i++)
    {
        int  l_key = _processcmdMap.keys()[i];
        st_cmd_attr l_cmd_attr = _processcmdMap[l_key];
        float lfdata =  cacldata(l_cmd_attr);
        _pProcessWidget->item(i,2)->setText(QString::number(lfdata,'f',4));
    }
}

/**
* @brief
* @note
*
*/
float QAxisWorkModeWidget::cacldata(st_cmd_attr cmd_attr)
{
    if(cmd_attr.ptrValue == nullptr)
    {
        return 0.0;
    }

    float lfdata = 0.0;
    float lfcoeff = 1.0/cmd_attr.lfCoeff;
    if(cmd_attr.i32Valuelen == 0x01)
    {
        uchar l_data;
        l_data = cmd_attr.ptrValue[0];
//        l_data.data[1] = cmd_attr.ptrValue[2];
//        l_data.data[2] = cmd_attr.ptrValue[1];
//        l_data.data[3] = cmd_attr.ptrValue[0];
        lfdata = l_data * lfcoeff;
    }

    if(cmd_attr.i32Valuelen == 0x02)
    {
        twochar l_data;
        l_data.data[0] = cmd_attr.ptrValue[1];
        l_data.data[1] = cmd_attr.ptrValue[0];
        if(cmd_attr.bsigned == true)
        {
            lfdata = l_data.i16data * lfcoeff;
        }
        else
        {
            lfdata = l_data.ui16data * lfcoeff;
        }
    }

    if(cmd_attr.i32Valuelen == 0x03)
    {
        fourchar l_data;
        l_data.data[0] = cmd_attr.ptrValue[2];
        l_data.data[1] = cmd_attr.ptrValue[1];
        l_data.data[2] = cmd_attr.ptrValue[0];
        l_data.data[3] = 0;
        lfdata = l_data.ui32data * lfcoeff;
    }

    if(cmd_attr.i32Valuelen == 0x04)
    {
        fourchar l_data;
        l_data.data[0] = cmd_attr.ptrValue[3];
        l_data.data[1] = cmd_attr.ptrValue[2];
        l_data.data[2] = cmd_attr.ptrValue[1];
        l_data.data[3] = cmd_attr.ptrValue[0];
        if(cmd_attr.bsigned == true)
        {
            lfdata = l_data.i32data * lfcoeff;
        }
        else
        {
            lfdata = l_data.ui32data * lfcoeff;
        }
    }

    return lfdata;
}


void QAxisWorkModeWidget::setButtonStyleSheet(QPushButton* pBtn)
{

    pBtn->setStyleSheet(
                "QPushButton:checked { background-color: rgb(245,90,70);;color:white;}"
                "QPushButton:unchecked { background-color: rgb(255,255,255);;color:black;}"
                "QPushButton{background-color: rgb(255,255,255);padding:5px;border-radius:5px;color:black;"
                "font-size:18px;font-family: 'Microsoft YaHei';}"
                "QPushButton:disabled { background-color: rgb(200,200,200);color:black;}"

                                );


//    pBtn->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    pBtn->setMinimumHeight(20);
}
